package ws

import (
	"github.com/gorilla/websocket"
	"sync"
)

type Visitor struct {
	Id     string
	Name   string
	Avator string
	Mux    sync.Mutex
	Conn   *websocket.Conn
}
type ChatRoom struct {
	sync.RWMutex
	members map[string][]*Visitor
}

func (r *ChatRoom) SendMessageToRoom(roomId string, msg string) {
	members, _ := r.GetMembers(roomId)
	for _, member := range members {
		member.Mux.Lock()
		member.Conn.WriteMessage(websocket.TextMessage, []byte(msg))
		member.Mux.Unlock()
	}
}
func (r *ChatRoom) GetMembers(key string) ([]*Visitor, bool) {
	r.RLock()
	value, ok := r.members[key]
	r.RUnlock()
	return value, ok
}

func (r *ChatRoom) SetMembers(key string, value []*Visitor) {
	r.Lock()
	r.members[key] = value
	r.Unlock()
}
